//
//  CustomAnnotationView.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/8/17.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import SDWebImage
import Nuke
import YSKit

extension CustomAnnotationView: Nuke.Target {
    public func handle(response: Result<Image>, isFromMemoryCache: Bool) {
        guard let image = response.value else {
            self.setAvator(UIImage(named: "空白头像"))
            return
        }
        self.setAvator(image)
    }
    public func setAvator(_ image: UIImage?) {
        self.image = image?.ys.circleImage(12.5)
        self.calloutView?.portraitView.image = image?.ys.circleImage(50)
    }
}
class CustomAnnotationView: MAAnnotationView {
    var calloutView: CustomCalloutView?
    var avatorUrlString: String? {
        didSet {
            if let s = avatorUrlString,  let url = URL(string: s) {
                Nuke.loadImage(with: url, into: self)

            }else {
                self.setAvator(UIImage(named: "空白头像"))
            }
            
        }
    }
    let kCalloutWidth = 200.0
    let kCalloutHeight = 70.0
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        calloutView?.frame = self.frame;
    }
    
    override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        calloutView?.frame = self.frame
        self.calloutView = CustomCalloutView(frame: CGRect(x: 0, y: 0, width: kCalloutWidth, height: kCalloutHeight))
        self.customCalloutView = MACustomCalloutView.init(customView: self.calloutView)
        self.calloutView?.setTitle(title: annotation.title!)
        self.calloutView?.setSubTitle(subTitle: annotation.subtitle!)
        self.calloutOffset = CGPoint(x:20, y: 0)

    }
    
}
